package cn.com.pansky.action.unit;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import net.vsame.url2sql.utils.PostParam;
import cn.com.pansky.helper.PostHelper;
import cn.com.pansky.sdk.API;
import cn.com.pansky.service.CommonService;
import cn.com.pansky.util.CardNumUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CaseAction extends CommonAction {

	/**
	 * 受理登记查询
	 */
	public void list() {
		JSONObject param = new JSONObject();
		int page = 1;
		if (null != context.getParam("PAGE")) {
			page = Integer.parseInt(context.getParam("PAGE"));
		}
		param.put("AAA121", "QNW85"); // 业务类型
		param.put("AAC002", "14262219920107153x"); // 加密字符串 约定是AAC002
		param.put("AAE011", user.getString("AA001")); // 代理人ID
		param.put("PAGE", page); // 页码
		param.put("PAGESIZE", 10); // 每页显示条数
		JSONObject object = API.sendController("acceptQueryController.json",
				param);
		if (object.getBooleanValue("SUCCESS")) {

			JSONArray array = object.getJSONArray("MSG");
			context.getDatas().put("result", array);
			datas.put("isPass", true);
		} else {
			datas.put("isPass", false);
			context.getDatas().put("result", new JSONArray());
		}
	}

	/**
	 * 根据身份证图片解析信息
	 */
	public void scanImg() {
		String imgPath = getParam("img"); // 身份证图片
		Model m = null;
		CommonService cs = new CommonService();
		JSONObject json = new JSONObject();
		try {
			json = cs.idendetify(imgPath);
		} catch (Exception e) {
			e.printStackTrace();
			datas.put("isPass", false);
			datas.put("info", "识别身份证失败！");
			context.getDatas().put("result", datas);
			return;
		}
		if (json.getBooleanValue("isOK")) {
			// 拿到身份证号，去好友列表中查询是否已是好友
			String sql = "SELECT a.ca001, a.ca002, a.ca003, a.ca006, a.ca009,(SELECT AAA103 FROM AA11 "
					+ "WHERE AAA100 = 'AAC005' AND AAA102 = A.CA051) CA051, decode(a.ca050, '1' , '男', '2', '女') CA050, a.ca052  from rc03 a WHERE a.ca006 = ?";
			m = SqlHelper.queryOne(sql, json.getString("CA006").toLowerCase());
			if(m == null) {
				m = new Model();
				m.put("CA002", "0");
				m.put("CA052", json.getString("CA052"));
				m.put("CA003", json.getString("CA003"));
				m.put("CA050", json.getString("CA050"));
				m.put("CA051", json.getString("CA051")+"族");
				m.put("CA006", json.getString("CA006").toLowerCase());
				m.put("CA009", json.getString("CA009"));
				
				String ca001 = UUID.randomUUID().toString();
				String sql1 = "INSERT INTO RC03(CA001, CA002, CA003, CA006, CA009, CA051, CA050, CA052) VALUES(?, '0', ?, ?, ?, ?, ?, ?)";// 获取sql
				// 保存自然人信息RC03
				SqlHelper.execute(sql1, ca001, json.getString("CA003"),
						m.getString("CA006"), json.getString("CA009"), cs.getValueByName("AAC005", json.getString("CA051")),
						cs.getAac004(json.getString("CA050")), 
						json.getString("CA052"));
				try {
					// 保存照片信息
					cs.isnertRC05(ca001, imgPath, "");
				} catch (Exception e) {
					e.printStackTrace();
				}
				m.put("CA001", ca001);
			}
			String[] AAC002 = {m.getString("CA006")};
			JSONArray jsonArray = API.getPersonState(AAC002); //请求业务系统接口
			if(jsonArray.size()>0) {
				JSONObject obj = (JSONObject)jsonArray.get(0);
				JSONObject jsonObj = obj.getJSONObject("ITEM");
				isRetired(m);
				m.put("CA006", m.getString("CA006").replace(m.getString("CA006").substring(1, 17), "****************"));
				m.put("AAC084", jsonObj.getString("AAC084")); // 享受待遇
				m.put("AAC031", jsonObj.getString("AAC031")); // 缴费状态
				m.put("AAE0160", jsonObj.getString("AAE0160")); // 参保状态
				m.put("MINYEAR", jsonObj.getString("MINYEAR"));
				m.put("AAE0161", jsonObj.getString("AAE0161")); // 待遇申请
				m.put("AAE0162", jsonObj.getString("AAE0162"));  // 转移申请
				m.put("ISDAOLIN", jsonObj.getString("ISDAOLIN")); // 到龄标志
				m.put("MAXYEAR", jsonObj.getString("MAXYEAR"));
			}
			m.put("isPass", true);
		} else {
			m = new Model();
			m.put("isPass", false);
			m.put("info", "识别身份证失败！");
		}
		context.getDatas().put("result", m);
		System.out.println(context.getDatas());
	}
	
	/**
	 * 获取人员是否是领取待遇人员，是否认证通过
	 * @param model
	 * @param CA003
	 * @param CA006
	 */
	private void isRetired(Model model) {
		List<PostParam> p = new ArrayList<PostParam>();
		PostParam AAC002 = new PostParam("AAC002", "string", model.getString("CA006"));// 身份证号
		PostParam AAC003 = new PostParam("AAC003", "string", model.getString("CA003"));// 姓名
		p.add(AAC002);
		p.add(AAC003);
		JSONObject results = PostHelper.send("auth/isNeedAuth.json", p);
		boolean isRetired = (Boolean) results.get("isRetired");
		boolean isAuthed = (Boolean) results.get("isAuthed");

		model.add("isRetired", isRetired ? 1 : 0);
		model.add("isAuthed", isAuthed ? 1 : 0);
	}

}
